package ru.paswd.marketprice.ui.widgets.chart.impl.philjay

import android.content.Context
import android.util.AttributeSet
import com.github.mikephil.charting.charts.LineChart
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.data.LineData
import com.github.mikephil.charting.data.LineDataSet
import ru.paswd.marketprice.ui.widgets.chart.PriceHistoryItem
import ru.paswd.marketprice.ui.widgets.chart.PriceHistoryView
import java.util.stream.Collectors

class PhilJayPriceHistoryView : PriceHistoryView {

    companion object {
        private const val SET_LABEL = "History"
        private const val TEXT_SIZE = 12f
        private const val CHART_OFFSET = 4f
        private const val LINE_WIDTH = 1f
    }

    private var chart: LineChart? = null

    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)

    override fun setItems(items: List<PriceHistoryItem>) {
        if (chart == null) {
            initChart()
        }

        val entries = items.stream()
            .map { i -> Entry(i.x.toFloat(), i.y.toFloat()) }
            .collect(Collectors.toList())

        val set = getDataSet()
        set.clear()

        set.values = entries

        chart?.data?.clearValues()
        chart?.data?.addDataSet(set)
        chart?.data?.notifyDataChanged()
    }

    private fun initChart() {
        chart = setWidget { LineChart(context) }
        chart?.data = LineData()

        chart?.description?.isEnabled = false

        chart?.extraBottomOffset = CHART_OFFSET
        chart?.setTouchEnabled(true)

        chart?.isDragEnabled = false
        chart?.isScaleXEnabled = false
        chart?.isScaleYEnabled = false
        chart?.setPinchZoom(false)
        chart?.setDrawGridBackground(false)
        chart?.isAutoScaleMinMaxEnabled = true

        chart?.xAxis?.enableGridDashedLine(10f, 10f, 0f)
        chart?.xAxis?.position = XAxis.XAxisPosition.BOTTOM
        chart?.xAxis?.labelCount = 4
        chart?.xAxis?.valueFormatter = PriceHistoryValueFormatter()
        chart?.xAxis?.textSize = TEXT_SIZE

        chart?.axisLeft?.setDrawGridLines(false)
        chart?.axisRight?.setDrawGridLines(false)
        chart?.axisLeft?.textSize = TEXT_SIZE
        chart?.axisRight?.textSize = TEXT_SIZE

        chart?.legend?.isEnabled = false
    }

    private fun getDataSet(): LineDataSet {
        return if (chart?.data?.dataSetCount ?: 0 > 0) {
           chart!!.data.getDataSetByIndex(0) as LineDataSet
        } else {
            val set = LineDataSet(null, SET_LABEL)
            set.mode = LineDataSet.Mode.CUBIC_BEZIER
            set.setDrawFilled(true)
            set.setCircleColor(getPrimaryColor())
            set.setDrawValues(false)
            set.lineWidth = LINE_WIDTH
            set.color = getPrimaryColor()
            set.fillDrawable = getGradient()
            set.fillAlpha = 100
            return set
        }
    }
}